//
//  ZLPullMenuCell.swift
//  ZLPullMenuSwift
//
//  Created by 科大 on 2025/1/16.
//

import UIKit

class ZLPullMenuCell: UICollectionViewCell {
    // MARK: - Properties
    
    /// 数据
    var item: ZLPullMenuItem? {
        didSet {
            updateUI()
        }
    }
    
    /// 标题按钮
    private lazy var titleButton: UIButton = {
        let button = UIButton(type: .custom)
        button.contentMode = .right
        button.setTitleColor(UIColor(white: 0.4, alpha: 1), for: .normal)
        let image = imageNamed("arrow_down")?.withRenderingMode(.alwaysTemplate)
        button.setImage(image, for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 14)
        button.isUserInteractionEnabled = false
        return button
    }()
    
    // MARK: - Initialization
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(titleButton)
        titleButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Private Methods
    
    private func updateUI() {
        guard let item = item else { return }
        
        if item.selected {
            titleButton.titleLabel?.font = item.config.selectedFont
            titleButton.setTitleColor(item.config.textSelectedColor, for: .normal)
            let image = imageNamed("arrow_up")?.withRenderingMode(.alwaysTemplate)
            titleButton.imageView?.tintColor = item.config.arrowSelectedColor
            titleButton.setImage(image, for: .normal)
        } else {
            titleButton.titleLabel?.font = item.config.font
            titleButton.setTitleColor(item.config.textColor, for: .normal)
            let image = imageNamed("arrow_down")?.withRenderingMode(.alwaysTemplate)
            titleButton.imageView?.tintColor = item.config.arrowColor
            titleButton.setImage(image, for: .normal)
        }
        
        titleButton.setTitle(item.menuTitle, for: .normal)
        
        // 设置标题和图片的边距
        if let imageSize = titleButton.imageView?.image?.size,
           let titleSize = titleButton.titleLabel?.bounds.size {
            titleButton.titleEdgeInsets = UIEdgeInsets(
                top: 0,
                left: -imageSize.width,
                bottom: 0,
                right: imageSize.width
            )
            
            titleButton.imageEdgeInsets = UIEdgeInsets(
                top: 0,
                left: titleSize.width,
                bottom: 0,
                right: -titleSize.width
            )
        }
    }
    
    private func imageNamed(_ name: String) -> UIImage? {
        let frameworkBundle = Bundle(for: type(of: self))
        guard let imageBundle = Bundle(path: frameworkBundle.path(forResource: "ZLPullMenuSwift", ofType: "bundle") ?? "") else {
            return UIImage()
        }
        
        return UIImage(named: name, in: imageBundle, compatibleWith: nil) ?? UIImage()
    }
}
